/**
* \file DGVMain.cpp
*
* \brief This file defines the class for the Main Window using only Qt.
* \author Shekhar S. Chandra, 2008
*
* This file is part of DGV Library.
*
* DGV is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DGV is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DGV.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "DGVMain.h"

#include <QFileDialog>

DGVMain::DGVMain(QWidget *parent) : QMainWindow(parent)
{
    ///Setup the UI according to the details in the Qt Designer generated header
    createMenu();

    ///Establish a workspace for user
    workspace = new QWorkspace(this);
    QMainWindow::setCentralWidget(workspace);
    windowMapper = new QSignalMapper(this);

    ///Setup Connections
    createConnections();
}

DGVMain::~DGVMain()
{

}

void DGVMain::addImage(DGVImage *img)
{
    workspace->addWindow(img);
    connect(img, SIGNAL(tableAvailable(DGVTable *)), this, SLOT(displayTable(DGVTable *)));
}

void DGVMain::addTable(DGVTable *tbl)
{
    workspace->addWindow(tbl);
    connect(tbl, SIGNAL(plotAvailable(DGVPlot *)), this, SLOT(displayPlot(DGVPlot *)));
}

void DGVMain::addPlot(DGVPlot *plot)
{
    workspace->addWindow(plot);
    //connect(img, tableAvailable(DGVTable *), this, displayTable(DGVTable *));
}

//Slots
void DGVMain::displayImage(DGVImage *img)
{
    ///Set the image into the viewer
    img->adjustSize();
    addImage(img);
    img->show();
}

void DGVMain::displayTable(DGVTable *newTable)
{
    ///Set the image into the viewer
    newTable->resizeColumnsToContents();
    newTable->adjustSize();
    addTable(newTable);
    newTable->show();
}

void DGVMain::displayPlot(DGVPlot *newPlot)
{
    ///Set the image into the viewer
    newPlot->adjustSize();
    addPlot(newPlot);
    newPlot->show();
}

void DGVMain::open()
{
    QFileDialog *fileOpener = new QFileDialog(this);
    QPixmap *pixmap = new QPixmap;

    QString filename = fileOpener->getOpenFileName(this,
                           tr("Select File to Open"),
                           ".",
                           tr("Images (*.png *.ppm *.pgm *.pbm *pnm *.tif *tiff *.jpeg *.jpg *.bmp)\nData Files (*.csv)"));

    if (!filename.isEmpty())
    {
        if(filename.contains(".png", Qt::CaseInsensitive) || filename.contains(".ppm", Qt::CaseInsensitive)
            || filename.contains(".pgm", Qt::CaseInsensitive) || filename.contains(".jpeg", Qt::CaseInsensitive)
            || filename.contains(".jpg", Qt::CaseInsensitive) || filename.contains(".bmp", Qt::CaseInsensitive))
        {
            DGVImage *image = new DGVImage(this);

            pixmap->load(filename);

            ///Set the image into the viewer
            image->setWindowTitle(filename);
            image->setPixmap(*pixmap);
            displayImage(image);
        }
        else if(filename.contains(".csv", Qt::CaseInsensitive))
        {
            DGVTable *table = new DGVTable(this);

            if(table->loadCSV(filename))
            {
                table->setWindowTitle(filename);
                displayTable(table);
            }
            else
            {
                QMessageBox::warning(this, "DGV Error",
                "Unable to load data file.\n"
                "Check the data Delimiters and Try opening the file again.");
            }
        }
    }
}

void DGVMain::save()
{
}

void DGVMain::newTable()
{
    bool ok;
    int rows = QInputDialog::getInteger(this, tr("Enter the Number of Rows of the table."),
                                          tr("New Rows:"), 23, 0, INT_MAX, 1, &ok);
    int cols = QInputDialog::getInteger(this, tr("Enter the Number of Columns of the table."),
                                          tr("New Columns:"), 23, 0, INT_MAX, 1, &ok);

    if(ok && rows > 0 && cols > 0)
    {
        DGVTable *table = new DGVTable(this);

        table->setWindowTitle("New Table");
        table->setRowCount(rows);
        table->setColumnCount(cols);
        for(int j = 0; j < rows; j ++)
            for(int k = 0; k < cols; k ++)
                table->setItem(j,k,0);
        displayTable(table);
    }
}

void DGVMain::newImage()
{
    bool ok;
    int rows = QInputDialog::getInteger(this, tr("Enter the Number of Rows of the image."),
                                          tr("New Rows:"), 23, 0, INT_MAX, 1, &ok);
    int cols = QInputDialog::getInteger(this, tr("Enter the Number of Columns of the image."),
                                          tr("New Columns:"), 23, 0, INT_MAX, 1, &ok);

    if(ok && rows > 0 && cols > 0)
    {
        DGVImage *image = new DGVImage(this);
        Array<imageType,2> newImageData(rows,cols);

        image->setWindowTitle("New Image");
        newImageData = 0;
        image->setPixmap(newImageData);
        displayImage(image);
    }
}

void DGVMain::cut()
{
}

void DGVMain::copy()
{
}

void DGVMain::paste()
{
}

void DGVMain::createMenu()
{
    menuBar = new QMenuBar(this);
    menuFile = new QMenu(menuBar);
    menuNew = new QMenu(menuBar);
    menuWindows = new QMenu(menuBar);
    actionOpen = new QAction(this);
    actionSave = new QAction(this);
    actionExit = new QAction(this);
    actionNewTable = new QAction(this);
    actionNewImage = new QAction(this);
    actionCascade = new QAction(this);
    actionTile = new QAction(this);

    ///Setup Exit Action and File Menu
    menuBar->addAction(menuFile->menuAction());
    menuFile->addAction(menuNew->menuAction());
    menuFile->setTitle(QApplication::translate("MainWindow", "File", 0, QApplication::UnicodeUTF8));
    menuNew->setTitle(QApplication::translate("MainWindow", "New", 0, QApplication::UnicodeUTF8));
    actionOpen->setText(QApplication::translate("MainWindow", "Open...", 0, QApplication::UnicodeUTF8));
    actionSave->setText(QApplication::translate("MainWindow", "Save...", 0, QApplication::UnicodeUTF8));
    actionExit->setText(QApplication::translate("MainWindow", "Exit", 0, QApplication::UnicodeUTF8));
    ///File
    menuFile->addAction(actionOpen);
    menuFile->addAction(actionSave);
    menuFile->addSeparator();
    menuFile->addAction(actionExit);
    ///New
    actionNewTable->setText(QApplication::translate("MainWindow", "New Table", 0, QApplication::UnicodeUTF8));
    actionNewImage->setText(QApplication::translate("MainWindow", "New Image", 0, QApplication::UnicodeUTF8));
    menuNew->addAction(actionNewTable);
    menuNew->addAction(actionNewImage);
    ///Windows
    menuBar->addAction(menuWindows->menuAction());
    menuWindows->setTitle(QApplication::translate("MainWindow", "Windows", 0, QApplication::UnicodeUTF8));
    actionCascade->setText(QApplication::translate("MainWindow", "Cascade", 0, QApplication::UnicodeUTF8));
    menuWindows->addAction(actionCascade);
    actionTile->setText(QApplication::translate("MainWindow", "Tile", 0, QApplication::UnicodeUTF8));
    menuWindows->addAction(actionTile);

    setMenuBar(menuBar);
}

void DGVMain::createConnections()
{
    QObject::connect(actionOpen, SIGNAL(activated()), this, SLOT(open()));
    QObject::connect(actionSave, SIGNAL(activated()), this, SLOT(save()));
    QObject::connect(actionExit, SIGNAL(activated()), this, SLOT(close()));
    QObject::connect(actionNewTable, SIGNAL(activated()), this, SLOT(newTable()));
    QObject::connect(actionNewImage, SIGNAL(activated()), this, SLOT(newImage()));
    QObject::connect(actionCascade, SIGNAL(activated()), workspace, SLOT(cascade()));
    QObject::connect(actionTile, SIGNAL(activated()), workspace, SLOT(tile()));
}
